package com.blog.action;

import com.blog.biz.UserBiz;
import com.blog.entity.User;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * Author:  Wu Yujie
 * Email:  coffee377@dingtalk.com
 * Time:  2017/02/16 00:27
 */
@Controller
//@Scope("singleton")
public class UserAction extends ActionSupport implements InitializingBean, DisposableBean {
    @Resource
    private UserBiz userBiz;

    @Resource
    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String loginUrl() {
        return SUCCESS;
    }

    public String homeUrl() {
        return SUCCESS;
    }

    public String userLogin() {
        User obj = (User) ServletActionContext.getRequest().getSession().getAttribute("USER");
//        User user1 = new User();
//        user1.setId(1);
//        user1.setUserName("123456");
//        user1.setPassword("123456");
//        ValueStack valueStack = ActionContext.getContext().getValueStack();
//        valueStack.push(user1);
//        User user11 = (User) valueStack.findValue("user1");
//        System.out.println(user11);
//        ActionContext.getContext().put("user1",user1);
        if (obj != null) {
            return SUCCESS;
        } else {
            User loginUser = userBiz.login(user.getUserName(), user.getPassword());
            System.out.println(loginUser);
            if (loginUser != null) {
                ServletActionContext.getRequest().getSession().setAttribute("USER", loginUser);
                return SUCCESS;
            }
            return INPUT;
        }

    }


    public String userLogout() {
        HttpSession httpSession = ServletActionContext.getRequest().getSession();
        userBiz.logout(httpSession);
//        user = null;


        return SUCCESS;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("初始化：" + this);
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("销毁：");
    }
}

